package com.micro.modual.service.impl;

import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.SeckillScene;
import com.micro.modual.extension.SeckillSceneExtension;
import com.micro.modual.mapper.SeckillActivitySenceMapper;
import com.micro.modual.mapper.SeckillDetailMapper;
import com.micro.modual.mapper.SeckillSceneMapper;
import com.micro.modual.model.bo.seckillscene.SeckillSceneBO;
import com.micro.modual.model.dto.seckillscene.SeckillSceneDetailDTO;
import com.micro.modual.service.SeckillSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
*  <h1>秒杀场次设置表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("seckillSceneService")
public class SeckillSceneServiceImpl extends AbstractBasicsServiceImpl<SeckillScene,Long> implements SeckillSceneService {

    @Autowired
    private SeckillSceneMapper seckillSceneMapper;

    @Autowired
    private SeckillActivitySenceMapper seckillActivitySenceMapper;

    @Autowired
    private SeckillDetailMapper seckillDetailMapper;

    /**
     * <h2>新增秒杀场次设置表详情</h2>
     * @param seckillSceneBO {@link SeckillSceneBO} 秒杀场次设置表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSeckillScene(SeckillSceneBO seckillSceneBO) {
        SeckillScene seckillScene = SeckillSceneExtension.converAddSeckillScene(seckillSceneBO);
        seckillSceneMapper.insertSelective(seckillScene);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }


    /**
     * <h2>根据主键编号删除秒杀场次设置表信息</h2>
     * @param id  秒杀场次设置表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSeckillScene(Long id) {
        SeckillScene seckillScene = seckillSceneMapper.selectByPrimaryKey(id);
        if(seckillActivitySenceMapper.hasInProgressSeckill(seckillScene.getSceneCode(),null) > 0){
            throw new BizException(MicroResponseCode.HAS_IN_PROGRESS_SECKILL_NOT_ALLOW_DELETE.getResponseCode(),MicroResponseCode.HAS_IN_PROGRESS_SECKILL_NOT_ALLOW_DELETE.getResponseMessage());
        }
        seckillActivitySenceMapper.deleteBySceneCode(seckillScene.getSceneCode());
        seckillDetailMapper.deleteBySceneCode(seckillScene.getSceneCode());
        seckillSceneMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>查询时间区间:排除已经关联的</h2>
     * @param seckillCode  秒杀活动编号
     * @return
     */
    @Override
    public RespJsonData<List<SeckillSceneDetailDTO>> findTimeSectionList(String seckillCode) {
        List<SeckillScene> seckillSceneList = seckillSceneMapper.findTimeSectionList(seckillCode);
        List<SeckillSceneDetailDTO> seckillSceneDetailDTOList = JacksonUtil.getList(JacksonUtil.toJson(seckillSceneList), SeckillSceneDetailDTO.class);
        return new RespJsonData<List<SeckillSceneDetailDTO>>(seckillSceneDetailDTOList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

}
